package com.desire.homework;

import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import static java.time.temporal.ChronoUnit.MONTHS;

/**
 * @author LittleDesire
 * @description
 * @date 2022-02-10 周四 19:39
 * @since 17
 */
public class _53_获取总的日期间隔 {
    public static void main(String[] args) {
        // 指定转换格式
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 将传入的字符串以fmt的定义的格式解析出来
        LocalDate startDate = LocalDate.parse("2019-03-01", fmt);
        LocalDate endDate = LocalDate.parse("2020-04-02", fmt);

        System.out.println("总相差的天数:" + startDate.until(endDate, ChronoUnit.DAYS));
        System.out.println("总相差的月数:" + startDate.until(endDate, MONTHS));
        System.out.println("总相差的年数:" + startDate.until(endDate, ChronoUnit.YEARS));
        //总相差的天数:398
        //总相差的月数:13
        //总相差的年数:1

        //求出两个日期之间的天数
        var after = LocalDate.of(1970, 1, 1);
        var now = LocalDate.now();
        Duration days = Duration.between(after.atStartOfDay(), now.atStartOfDay());
        //13257 天
        System.out.println(days.toDays());

        long ddd = ChronoUnit.DAYS.between(after, now);
        //13257 天
        System.out.println(ddd);

        //435个月
        System.out.println(MONTHS.between(after, now));
        //36年
        System.out.println(ChronoUnit.YEARS.between(after, now));
    }
}
